//
//  MeBindInfoVC.m
//  Ginix
//
//  Created by 冯志强 on 2025/2/24.
//

#import "MeBindInfoVC.h"
#import "MeSetNameView.h"

@interface MeBindInfoVC ()
@property (nonatomic,strong) UILabel * infoLabel;
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic,strong) UIButton * enterBtn;
@property (nonatomic,strong) HomeTipsView * tipsView;


@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UIButton * joinbtn;
@property (nonatomic,strong) MeSetNameView * setNameView;
@end

@implementation MeBindInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSubViewLayout];
    [self requestData];
}

-(void)setSubViewLayout{
    
    self.title = @"设备管理";
    self.navgationView.infoLabel.textColor = RDRGB(0x714933);
    
    [self.view addSubview:self.infoLabel];
    [_infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.top.mas_offset(NavHeight + 20 * FMScaleX);
        make.left.mas_offset(30 * FMScaleX);
    }];
    [self.view addSubview:self.iconView];
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(150 * FMScaleX, 150 * FMScaleX));
        make.center.equalTo(@0);
    }];
    [self.view addSubview:self.enterBtn];
    [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(330 * FMScaleX, 50 * FMScaleX));
        make.centerX.equalTo(@0);
        make.bottom.mas_offset(-kSafeAreaHeight - 50 * FMScaleX);
    }];
    [self.view addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(30 * FMScaleX);
        make.top.equalTo(self.infoLabel.mas_bottom).offset(15 * FMScaleX);
    }];
    
    [self.view addSubview:self.joinbtn];
    [_joinbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(120 * FMScaleX, 30 * FMScaleX));
        make.centerY.equalTo(self.nameLabel);
        make.right.mas_offset(-30 * FMScaleX);
    }];
}

-(void)requestData{
    
    if (UserInfoModel.shareInstance.userModel.isBind == 1) {
        self.joinbtn.hidden = NO;
        self.nameLabel.text = UserInfoModel.shareInstance.userModel.deviceName;
        [self.enterBtn setTitle:@"解绑设备" forState:UIControlStateNormal];
    }else{
        self.joinbtn.hidden = YES;
        self.iconView.hidden = YES;
        self.nameLabel.text = @"暂无绑定设备";
        [self.enterBtn setTitle:@"绑定设备" forState:UIControlStateNormal];
    }
}

-(void)enterBtnAction:(UIButton *)sender{
    
    
    if ([sender.titleLabel.text isEqual:@"解绑设备"]) {
        @WeakObj(self)
        [[BCLRingManager shared] getBlcConnectWithCompletion:^(BOOL state) {
            @StrongObj(self)
            if (state) {
                [self.tipsView startAnimationWithTitle:@"解绑设备" info:@"是否解绑当前设备？"];
                self.tipsView.enterdBtnBlock = ^{
                    [SPNetworkingIns POST:PORT(@"user-center/api/appUser/userUnbind") params:@{} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
                        [[BCLRingManager shared]clearRingDataWithCompletion:^(BOOL success) {
                            @StrongObj(self)
                            if (success) {
                                FhudShowStr(@"解绑成功")
                                [[BCLRingManager shared] disconnect];
                                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                    [self.navigationController popViewControllerAnimated:YES];
                                });
                            }
                        }];
                    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                        
                    }];
                };
            }else{
                FhudShowStr(@"请先连接设备")
            }
        }];
    }
    
    if ([sender.titleLabel.text isEqual:@"绑定设备"]) {
        id vc = [[NSClassFromString(@"HomeBindVC") alloc]init];
        [self.navigationController pushViewController:vc animated: YES];
    }
        
}

-(void)joinbtnAction{
    
    
    [self.setNameView startAnimationWithType:4];
    @WeakObj(self)
    self.setNameView.enterdBtnBlock = ^(NSString * _Nonnull deviceName, NSInteger type) {
        [[BCLRingManager shared]getBlcConnectWithCompletion:^(BOOL state) {
            if (state) {
                [[BCLRingManager shared] setBluetoothNameWithName:deviceName completion:^(BOOL state, NSString * _Nullable name) {
                    if (state) {
                        @StrongObj(self)
                        UserInfoModel * model = UserInfoModel.shareInstance.userModel;
                        NSDictionary * parm = @{@"mac":model.mac,@"uuid":model.uuid,@"deviceName":deviceName};
                        [SPNetworkingIns POST:PORT(@"user-center/api/appUser/userUpdateDevice") params:parm success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
                            FhudShowStr(@"修改成功");
                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                [self.navigationController popViewControllerAnimated:YES];
                            });
                        } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
                            
                        }];
                    }
                }];
            }else{
                FhudShowStr(@"请线连接设备")
            }
        }];
    };
            
}

-(UILabel *)infoLabel{
    
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]init];
        _infoLabel.font = Regular(14);
        _infoLabel.textColor = RDRGB(0x714933);
        _infoLabel.numberOfLines = 0;
        _infoLabel.text = @"当前绑定设备:";
    }
    return _infoLabel;
}
-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"Home_jiezhi_icon"];
    }
    return _iconView;
}
-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]init];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Common_btn_bg"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"解绑设备" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _enterBtn.titleLabel.font = Regular(14);
    }
    return _enterBtn;
}

-(HomeTipsView *)tipsView{
    
    if (!_tipsView) {
        _tipsView = [[HomeTipsView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:_tipsView];
    }
    return _tipsView;
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = Regular(16 * FMScaleX);
        _nameLabel.textColor = RDRGB(0xB6904A);
    }
    return _nameLabel;
}
-(UIButton *)joinbtn{
    
    if (!_joinbtn) {
        _joinbtn = [[UIButton alloc]init];
        [_joinbtn setBackgroundImage:[UIImage imageNamed:@"Home_join_icon_no"] forState:UIControlStateNormal];
        [_joinbtn setTitle:@"更改设备名称" forState:UIControlStateNormal];
        [_joinbtn setTitleColor:RDRGB(0xB6904A) forState:UIControlStateNormal];
        [_joinbtn addTarget:self action:@selector(joinbtnAction) forControlEvents:UIControlEventTouchUpInside];
        _joinbtn.titleLabel.font = Regular(13 * FMScaleX);
    }
    return _joinbtn;
}
-(MeSetNameView *)setNameView{
    
    if (!_setNameView) {
        _setNameView = [[MeSetNameView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:_setNameView];
    }
    return _setNameView;
}
@end
